package pacman.entries.pacman.meta_graph;

import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import com.sun.org.apache.regexp.internal.recompile;

import pacman.game.Game;
import pacman.game.Constants.MOVE;

public class MetaGraphNode implements IMetaGraphObject {

	MOVE[] m_Moves = { MOVE.UP, MOVE.DOWN, MOVE.LEFT, MOVE.RIGHT };
	     
	int m_Index;
	public int MazeIndex() { return m_Index; }
	
	public MetaGraphNode(int index) {
		m_Index = index;
		m_AdjacentMetaNodes = new MetaGraphNode[4];
		m_AdjacentMetaEdges = new MetaGraphEdge[4];
		Arrays.fill(m_AdjacentMetaNodes, null);
		Arrays.fill(m_AdjacentMetaEdges, null);
		found = false;
	}
	
	@Override
	public float calcScore(Game game, MetaGraphEvaluator evaluator) {
		
		return evaluator.Evaluate(game, this);
	}
	
	@Override
	public boolean equals(Object obj) {
		if ( !(obj instanceof MetaGraphNode) ) return false;
		return ((MetaGraphNode) obj).m_Index == m_Index;
	}
	
	// __________________________________________________________
	
	// creates an edge between two nodes and returns the new edge instance
	// each direction is the MOVE to make to go towards the opposite node
	static MetaGraphEdge CreateEdgeBetween(MetaGraphNode node1, MOVE direction1, MetaGraphNode node2, MOVE direction2) {
		MetaGraphEdge edge = new MetaGraphEdge(node1, node2);
		int direction1int = direction1.ordinal();
		int direction2int = direction2.ordinal();
		node1.m_AdjacentMetaEdges[direction1int] = edge;
		node2.m_AdjacentMetaEdges[direction2int] = edge;
		node1.m_AdjacentMetaNodes[direction1int] = node2;
		node2.m_AdjacentMetaNodes[direction2int] = node1;
		return edge;
	}
	
	private MetaGraphNode[] m_AdjacentMetaNodes;
	private MetaGraphEdge[] m_AdjacentMetaEdges;
	public boolean found;
	public int getIndex() {
		return m_Index;
	}
	// move is being used as the direction from the corner
	// returns -1 if there is no corner in the given direction
	public int getPillCount(MOVE move) {
		MetaGraphEdge edge = m_AdjacentMetaEdges[move.ordinal()];
		if(edge != null) {
			return edge.getPillCount();
		}
		return -1;
	}
	public void setPillCount(MOVE move, int count) {
		MetaGraphEdge edge = m_AdjacentMetaEdges[move.ordinal()];
		if(edge != null) {
			edge.setPillCount(count);
		}
	}
	public int decrementPillCount(MOVE move) {
		MetaGraphEdge edge = m_AdjacentMetaEdges[move.ordinal()];
		if(edge != null) {
			return edge.decrementPillCount();
		}
		return -1;
	}
	public MetaGraphNode getAdjacentMetaNode(MOVE move) {
		return m_AdjacentMetaNodes[move.ordinal()];
	}
	
	public Collection<MetaGraphNode> getAdjacentMetaNodes() {
		List<MetaGraphNode> nodeList = new LinkedList<MetaGraphNode>();
		
		MetaGraphNode node = null;
		
		for (int i = 0; i < m_Moves.length; i++) {
			node = getAdjacentMetaNode(m_Moves[i]);
			if (node != null)
				nodeList.add(node);
		}
		
		return nodeList;
	}
	
	public MetaGraphEdge getAdjacentMetaEdge(MOVE move) {
		return m_AdjacentMetaEdges[move.ordinal()];
	}
	
	public Collection<MetaGraphEdge> getAdjacentMetaEdges() {
		List<MetaGraphEdge> edgeList = new LinkedList<MetaGraphEdge>();
		
		MetaGraphEdge edge = null;
		
		for (int i = 0; i < m_Moves.length; i++) {
			edge = getAdjacentMetaEdge(m_Moves[i]);
			if (edge != null)
				edgeList.add(edge);
		}
		
		return edgeList;
	}
	// returns NEUTRAL if the node is not connected to this node
	public MOVE getMoveForAdjacentMetaNode(MetaGraphNode node) {
		for(int i = 0; i<4; i++) {
			MetaGraphNode compare = m_AdjacentMetaNodes[i];
			if(node.equals(compare)) {
				return MOVE.values()[i];
			}
		}
		return MOVE.NEUTRAL;
	}
	// returns NEUTRAL if the edge is not connected to this node
	public MOVE getMoveForMetaEdge(MetaGraphEdge edge) {
		MetaGraphNode[] nodes = edge.getNodes();
		if(equals(nodes[0])) {
			return getMoveForAdjacentMetaNode(nodes[1]);
		}
		if(equals(nodes[1])) {
			return getMoveForAdjacentMetaNode(nodes[0]);
		}
		return MOVE.NEUTRAL;
	}
	// returns the edge between this node and the given node
	// returns null if no such edge exists
	public MetaGraphEdge getEdgeBetween(MetaGraphNode node) {
		int index = node.getIndex();
		int moveIndex = -1;
		for(int i=0; i<4; i++) {
			if(node.equals(m_AdjacentMetaNodes[i])) {
			//if(m_AdjacentMetaNodes[i].getIndex() == index) {
				moveIndex = i;
				break;
			}
		}
		if(moveIndex < 0) return null;
		return m_AdjacentMetaEdges[moveIndex];
	}
}
